/* eslint-disable no-case-declarations */
const Response = require('../utils/Response');
const SSA = require('../model/SSA');
const Transaction = require('../model/Transaction');
const {
	SSA_CHAINCODE_NAME,
	SSA_CHAINCODE_ID,
	EARTH_CHAINCODE_ID,
} = require('../utils/Constants');
const logger = require('../utils/Logger').getLogger('handler');
const SchemaCheker = require('../utils/SchemaChecker');

class SSAHandler {
	/**
   * Sample code for create new contract account on Earth chaincode
   * This method will create an account of type "contract" at Earth
   *
   * @param {ChaincodeStub} stub
   * @param {string} contractAccountId the contract account for this Dapp at Earth
  */
	static async Create(stub, params) {
		const method = 'Create';
		logger.debug('%s - enter', method);
		try {
	      logger.enter(method);
	      if (params.length !== 1) {
	        logger.error('%s - Create new User requires params of length 1, found %s, %j', method, params.length, params);
	        return Response(false, 'Create new User requires params of length 1');
	      }
	      const req = JSON.parse(params[0]);
	      const createRequest = {
	        id: req.id,
	        name: req.name,
	        role: 'ssa',
	      };
	      logger.debug('%s - Create new ssa with options %j', method, createRequest);
	      const ssa = new SSA(stub);
	      const schema = [
	        { name: 'id', type: 'string', required: true },
	        { name: 'name', type: 'string', required: true },
	        { name: 'ssa', type: 'string', required: false },
	      ];
	      SchemaCheker.check(schema, createRequest);
	      logger.debug('%s - Create new ssa with options %j', method, createRequest);
	      await ssa.create(createRequest);
	      logger.debug('%s - Successfully create new User in bc, response: %j', method, ssa);
	      return Response(true, ssa.toString());
	    } catch (e) {
	      logger.error('%s - Error: %o', method, e);
	      return Response(false, e.message);
    	}
    }

	static async Query(stub, params) {
		const method = 'Query';
		try {
			logger.enter(method);
			logger.debug('%s - params: %j', method, params);
			if (params.length !== 1) {
				logger.error('%s - Query requires params of length 1, found %s, %j', method, params.length, params);
				return Response(false, 'Query requires params of length 1');
			}
			let ssa;
			const ssaRequest = JSON.parse(params[0]);
			logger.debug('%s - query ssaRequest: %j', method, ssaRequest);
			ssa = new SSA(stub);
			ssa = await ssa.getOneSSA(ssaRequest.id);
			logger.exit(method);
			return Response(true, ssa.toString());
		} catch (e) {
			logger.error('%s - Error: %s', method, e);
			return Response(false, e.message);
		}
	}

	static async Support(stub, params) {
		const method = 'Support';
		try {
			logger.enter(method);
			logger.debug('%s - params: %j', method, params);
			if (params.length !== 1) {
				logger.error('%s - Query requires params of length 1, found %s, %j', method, params.length, params);
				return Response(false, 'Query requires params of length 1');
			}
			let req = JSON.parse(params[0]);
			let ssa = new SSA(stub);
			ssa = await ssa.getOneSSA(req.ssaID);
			const tx = new Transaction(stub);
			req = {
				id: req.id,
				type: method,
				accountId: req.accountId,
				ssaID: req.ssaID,
				amount: req.amount,
				description: req.description,
			}
			await tx.create(req);
			logger.debug('%s - tx: %j', method, tx);
			await ssa.addSupportsRecord(tx);
			logger.exit(method);
			return Response(true, ssa.toString());
		} catch (e) {
			logger.error('%s - Error: %s', method, e);
			return Response(false, e.message);
		}
	}
}

module.exports = SSAHandler;
